<?
 
 class Controller_Forgot
  extends Controller_Base{
  	
  	function index($args) {
  		
  		$template = new Template('forgot.tmpl');
  		$template->setArg('do_not_show_login_form',1);
  		if(isset($_GET['ok'])) {
  			$template->setArg('do_not_show_login_form',0);
  			$template->setArg('show_success_message', 1);
  			$template->setArg('redirect', '/');
  		}
  		
  		if($_POST[action] === 'restore') {
  			
	 		$sigs = array(
	 			'email'		=> array('type' => 'string','function'=>array('htmlspecialchars'),'required' =>1),
				'username'	=> array('type' => 'string','function'=>array('htmlspecialchars'),'required' =>1),	 			
			);			
			
			sanitize_vars($_POST,$sigs,null);
			
			try {


				if(empty($_POST[username]))
					throw new UserException ("Введите имя пользователя!");

				if(empty($_POST[email]))
					throw new UserException ("Введите email!");

				$user = new Model_Externaluser($this->registry);
					
				$result = $user->generateNewPassword($_POST[username],$_POST[email]);
					
			} catch ( UserException $e ) {
				$template->setArg('error_text',$e->getMessage());
			} catch (PDOException $e) {
				error_log ("DB Error![add external user session]: " . $e->getMessage());		
			} catch (Exception $e) {
				error_log ("ERROR:". $e->getMessage());
			}
			
			if($result) {
				return array(
					status 		=> 302,
					location	=> "/forgot?ok"
				);

			}
			
			  			
  		}
  		$template->setArg(menu, $this->menu->getMenu(1));
  		return array(
			status 		=> 0,
			template	=> $template
  		);
  	}
  	
  }
